package com.yonyou.brigade.third_parties.tuniu.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.yonyou.brigade.basic_init.model.BriChannel;
import com.yonyou.brigade.basic_init.props.PropUtil;
import com.yonyou.brigade.basic_init.service.IBriChannelService;
import com.yonyou.brigade.basic_init.service.IUserCenterService;
import com.yonyou.brigade.third_parties.tuniu.model.TnDeptHistory;
import com.yonyou.brigade.third_parties.tuniu.model.TnUserHistory;
import com.yonyou.brigade.third_parties.tuniu.service.ITnDeptHistoryService;
import com.yonyou.brigade.third_parties.tuniu.service.ITnUserHistoryService;
import com.yonyou.brigade.third_parties.tuniu.service.ITuniuPublicService;
import com.yonyou.brigade.third_parties.tuniu.service.ITuniuUserService;
import com.yonyou.ybzboot.uc.dto.UcUserDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * Created by Administrator on 2018/11/6.
 */
@Service
public class TuniuUserServiceImpl implements ITuniuUserService{

    @Autowired
    @Qualifier("tnUserHistoryServiceImpl")
    ITnUserHistoryService tnUserHistoryService;

    @Autowired
    @Qualifier("briChannelServiceImpl")
    IBriChannelService iBriChannelService;

    @Autowired
    private PropUtil propUtil;

    @Autowired
    @Qualifier("tuniuPublicServiceImpl")
    private ITuniuPublicService tuniuPublicService;

    @Autowired
    @Qualifier("userCenterServiceImpl")
    private IUserCenterService userCenterService;

    @Autowired
    @Qualifier("tnDeptHistoryServiceImpl")
    private ITnDeptHistoryService tnDeptHistoryService;

    private static final Logger logger = LoggerFactory.getLogger(TuniuUserServiceImpl.class);

    @Override
    public String loginUrl(String tenant, String org, String phone, String pageType, String pcOrH5, String jsonExt) {
        String resultUrl= "";
        BriChannel briChannel = iBriChannelService.getEnableInfoByTenantOrg(tenant,org,"tuniu");
        if(briChannel==null) {
            resultUrl = propUtil.getNO_ACCESS_URL();
            logger.info("该组织没有开通途牛商旅服务！ tenant:" + tenant + " org:" + org);
            return resultUrl;
        }else {
            Wrapper<TnUserHistory> wrapper = new EntityWrapper<>();
            wrapper.eq(TnUserHistory.PHONE,phone);
            List<TnUserHistory> tnUserHistories = tnUserHistoryService.selectList(wrapper);
            if (BriChannel.SYNCUSER == briChannel.getSyncUser()) {
                if (tnUserHistories == null || tnUserHistories.size() == 0){
                    //如果当前用户没有同步到途牛，则进行同步
                    syncSingleUserToTuniu(tenant,org,phone,briChannel.getConfig());
                }
            }
            tnUserHistories = tnUserHistoryService.selectList(wrapper);
            if (tnUserHistories == null || tnUserHistories.size() == 0){
                resultUrl= propUtil.getNO_ACCESS_URL();
                return resultUrl;
            }

            resultUrl = tuniuPublicService.ssoLogin(tenant, tnUserHistories.get(0).getUserNo(), briChannel.getConfig(), pcOrH5);
        }

        return resultUrl;
    }

    public void syncSingleUserToTuniu(String tenant, String orgpk, String phone, String config) {
        logger.info("开始初始化同步途牛商旅人员信息 phone:" + phone);
        UcUserDto ucUserDto = userCenterService.getCompleteUcUserDtoByPhone(phone, tenant);
        if (ucUserDto != null){
            Map employeeData = new HashMap<>();
            String uid = tnUserHistoryService.getUidByPersonCode(ucUserDto.getPersonCode());
            if (!StringUtils.isEmpty(uid)){
                //更新用户信息
                employeeData.put("employeeName",ucUserDto.getUserName());
                employeeData.put("phone",ucUserDto.getPhone());
                employeeData.put("employeeNo",ucUserDto.getPersonCode());
                employeeData.put("uid",uid);

                tuniuPublicService.updateUser(employeeData,config);
            } else {
                //新增用户
                employeeData.put("employeeName",ucUserDto.getUserName());
                employeeData.put("employeeNo",ucUserDto.getPersonCode());
                employeeData.put("phone",ucUserDto.getPhone());
                employeeData.put("sex",0);

                Wrapper<TnDeptHistory> wrapper = new EntityWrapper<>();
                wrapper.eq(TnDeptHistory.TENANT,tenant);
                wrapper.eq(TnDeptHistory.ORGPK,orgpk);
                wrapper.eq(TnDeptHistory.DEPT_LEVEL,1);
                List<TnDeptHistory> tnDeptHistories = tnDeptHistoryService.selectList(wrapper);

                if (tnDeptHistories != null && tnDeptHistories.size() > 0){
                    employeeData.put("deptId",tnDeptHistories.get(0).getDeptId());
                }

                tuniuPublicService.insertUser(employeeData,config);
            }
        }
    }
}